package com.pine.sunshineelder.login.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.pine.sunshineelder.common.StringHelper;
import com.pine.sunshineelder.common.dao.AccountDao;
import com.pine.sunshineelder.common.dto.UserContext;
import com.pine.sunshineelder.common.entity.AccountEntity;
import com.pine.sunshineelder.common.exception.ThisSystemException;
import com.pine.sunshineelder.common.validator.ValidateHelper;
import com.pine.sunshineelder.login.service.LoginService;


@Service
public class LoginServiceImpl implements LoginService,StringHelper{
	@Autowired
	AccountDao adao;

	@Override
	public UserContext login(String account1, String password1) throws Exception {
		//1、验证和清理参数
				String account=ValidateHelper.$("账号", account1);
				String password=ValidateHelper.$("密码", password1);
				
				//2、执行业务逻辑
				//2.1 账号是否存在
				AccountEntity a=adao.selectByAccount(account);
				if(a==null) {
					throw new ThisSystemException("login.not-match","账号和密码不匹配");
				}
				
				//2.2 账号密码是否匹配
				if(!password.equals(a.getPassword())) {
					throw new ThisSystemException("login.not-match","账号和密码不匹配");
				}
				
				//3、组装业务结果
				UserContext result=new UserContext();
				result.setAccount(a.getAccount());
				result.setFirstLogin(a.getLastLoginTime()==null);
				result.setId(a.getId());
				result.setName(a.getName());
				result.setRole(a.getRole());
				return result;
	}

}
